﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.SharePoint.Commands;
using SPVSX.Explorer.Extensions;
using Microsoft.SharePoint;
using SPVSX.Explorer.Commands.SharePoint;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Commands
{
    class CustomActionCommands
    {
        // Gets data for each Custom Action on the SharePoint site, and returns an array of 
        // serializable objects that contain the data.
        [SharePointCommand(CustomActionCommandIds.GetCustomActions)]
        private static CustomActionNodeInfo[] GetCustomActions(ISharePointCommandContext context)
        {
            var nodeInfos = new List<CustomActionNodeInfo>();

            //Here is where this will call out to the smarts from SAS but for now create some dummy data
            ElementProvider availableProvider = ElementProvider.GetAvailableProvider();

            List<CustomActionElement> allActions =
                availableProvider.QueryForAllCustomActions(context.Web, SPFeatureScope.Farm.ToString());

            foreach (CustomActionElement item in allActions)
            {
                CustomActionNodeInfo info = new CustomActionNodeInfo();
                if (!String.IsNullOrEmpty(item.Id))
                {
                    info.Name = item.Id;
                }
                nodeInfos.Add(info);
            }

            

            return nodeInfos.ToArray();
        }

        // Gets additional property data for a specific Custom Action.
        [SharePointCommand(CustomActionCommandIds.GetCustomActionProperties)]
        private static Dictionary<string, string> GetCustomActionProperties(ISharePointCommandContext context,
            CustomActionNodeInfo nodeInfo)
        {
            //As the get of the info object is already expensive here we simply read back the properties from it
            //rather than harvest it again

            return SharePointCommandServices.GetProperties(nodeInfo);
        }
    }
}